<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Focussed 2D Array With Directional Elements Example (k-Wave)</title>
	<link rel="stylesheet" href="kwavehelpstyle.css" type="text/css">
	<meta name="description" content="Focussed 2D Array With Directional Elements Example.">
</head>

<body><div class="content">

<h1>Focussed 2D Array With Directional Elements Example</h1>

<p>This example demonstrates the use of k-Wave to compute the outputs from a curved detector array which consists of several elements, each of which consists of a number of grid points.</p>

<p>
    <ul>
        <li><a href="matlab:edit([getkWavePath('examples') 'example_sd_directional_array_elements.m']);" target="_top">Open the file in the MATLAB Editor</a></li>
        <li><a href="matlab:run([getkWavePath('examples') 'example_sd_directional_array_elements']);" target="_top">Run the file in MATLAB</a></li>
    </ul>
</p>

<h2>Contents</h2>
<div>
	<ul>
		<li><a href="#heading2">Define a focussed array of directional elements</a></li>
		<li><a href="#heading3">Using an infinitely wide line source</a></li>
		<li><a href="#heading4">Running the simulation</a></li>
		<li><a href="#heading5">Plotting</a></li>
	</ul>
</div>

<a name="heading2"></a>
<h2>Define a focussed array of directional elements</h2>

<p>To start with, a curved detector array is defined using the <code><a href="makeCircle.html">makeCircle</a></code> function.</p>

<pre class="codeinput">
<span class="comment">% define a semicircular sensor centred on the grid</span>
semicircle_radius = 65; % [grid points]
arc = makeCircle(Nx, Ny, Nx/2, Ny/2, semicircle_radius, pi);
</pre>

<p>The grid points making up this curved array are divided up between a number of elements. This is achieved by calculating the angles from each grid point to the focus of the array and grouping the grid points with similar angles.</p>

<pre class="codeinput">
<span class="comment">% find total number and indices of the grid points constituting the
% semicircle</span>
arc_indices = find(arc == 1);
Nv = length(arc_indices);

<span class="comment">% calculate angles between grid points in the arc and the centre of the
% grid</span>
arc_angles = atan((kgrid.y(arc_indices)) ./ kgrid.x(arc_indices));

<span class="comment">% sort the angles into ascending order, and adjust the indices accordingly</span>
[sorted_arc_angles, sorted_index] = sort(arc_angles);
sorted_arc_indices = arc_indices(sorted_index);

<span class="comment">% divide the semicircle into Ne separate sensor elements</span>
Ne = 13;
sensor.mask = zeros(Nx, Ny);
for loop = 1:Ne
    
    <span class="comment">% get the indices of the grid points belonging to the current element
    % (there is a two grid point gap between the elements)</span>
    voxel_indices = sorted_arc_indices(floor((loop - 1) * Nv / Ne) + ...
        2:floor(loop * Nv / Ne) - 1);
    
    <span class="comment">% add the element to the sensor.mask</span>
    sensor.mask(voxel_indices) = 1;
    
end
</pre>

<img vspace="5" hspace="5" src="images/example_sd_directional_array_elements_01.png" style="width:560px;height:420px;" alt="">

<a name="heading3"></a>
<h2>Using an infinitely wide line source</h2>

<p>To model a plane wave source parallel to one of the grid dimensions, two steps are necessary. First, <code>source.p_mask</code> is set so that it stretches across the domain.</p>

<pre class="codeinput">
source.p_mask = zeros(Nx, Ny);
source.p_mask(140, :) = 1;
</pre>

<p>Secondly, the inputs to the simulation are set so that the perfectly matched layer is turned off on the two sides that <code>source.p_mask</code> meets.</p>

<pre class="codeinput">
input_args = {'PMLAlpha', [2, 0], 'DisplayMask', display_mask, 'PlotScale', [-0.75, 0.75]};
</pre>

<a name="heading4"></a>
<h2>Running the simulation</h2>

<p>The simulation is run and the time series are measured for every non-zero grid point in <code>sensor.mask</code>. These time series are then allocated to their relevant elements, and averaged together.</p>

<pre class="codeinput">
sensor_data = kspaceFirstOrder2D(kgrid, medium, source, sensor, input_args{:});

<span class="comment">% split up the recorded data between the elements</span>
element_data = zeros(Ne, kgrid.Nt);
for loop = 1:Ne
    
    <span class="comment">% find indices of the sensor grid points in the sensor mask</span>
    sensor_indices = find(sensor.mask == 1);
    
    <span class="comment">% extract indices of the grid points belonging to the current element</span>
    voxel_indices = sorted_arc_indices(floor((loop - 1) * Nv / Ne) + ...
        2:floor(loop * Nv / Ne) - 1);
    
    <span class="comment">% indices of sensor_data that refer to the data for this element</span>
    data_indices = zeros(length(voxel_indices), 1);
    for loop2 = 1:length(voxel_indices)
        data_indices(loop2) = find(sensor_indices == voxel_indices(loop2));
    end    

    <span class="comment">% for one element per loop, average the time series from each of the
    % element's grid points to give one time series for the whole element</span>
    element_data(loop, :) = mean(sensor_data(data_indices, :), 1);
    
end
</pre>

<img vspace="5" hspace="5" src="images/example_sd_directional_array_elements_02.png" style="width:560px;height:420px;" alt="">

<p>The time series corresponding to the different elements are plotted. The directionality introduced by the large size of the detector (because it averages the incoming wave over its area) is clearly seen.</p>
    
<img vspace="5" hspace="5" src="images/example_sd_directional_array_elements_03.png" style="width:560px;height:420px;" alt="">
	
</div></body></html>